package com.zyc.app.woa.utils;

import android.app.ActionBar;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import com.zyc.app.woa.fragment.BaseFragment;

/**
 * Author: yingchang.zhang
 * Date  : 2014/8/13
 */
public class FragmentUtil {
    public static final String HOME_FRAGMENT = "home_fragment";
    public static final String CLEAR_TOP = "clear_top";


    public static void launchFragment(FragmentManager manager, int viewId, boolean addStack, BaseFragment fragment) {
        final String tag = fragment.getFragmentTag();
        Fragment prevFragment = manager.findFragmentByTag(tag);
        Trace.log("FragmentUtil.launchFragment prevFragment: " + prevFragment);

        FragmentTransaction transaction = manager.beginTransaction();
        if (null == prevFragment) {
            transaction.replace(viewId, fragment, tag);
            if (addStack) {
                transaction.addToBackStack(tag);
            }
            if (hasTag(tag, CLEAR_TOP)) {
                //启动时清空之前的所有fragment
                manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }
            transaction.commitAllowingStateLoss();
        } else {
            if (hasTag(tag, HOME_FRAGMENT)) {
                manager.popBackStack(tag, 0);
                //回到主fragment时，需要清空堆栈内的所有fragment
                manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            } else {
                manager.popBackStack(tag, 0);
            }
        }
    }

    public void clearBackStack(FragmentManager manager) {
        for (int i = 0; i < manager.getBackStackEntryCount(); i++) {
            manager.popBackStack();
        }
    }

    private static boolean hasTag(String fragmentTag, String specTag) {
        return (null != fragmentTag && fragmentTag.endsWith(specTag));
    }

    public void setActionBarVisible(Activity activity, boolean visible) {
        if (null != activity) {
            ActionBar actionBar = activity.getActionBar();
            if (null != actionBar) {
                if (visible) {
                    actionBar.show();
                } else {
                    actionBar.hide();
                }
            }
        }
    }

    public void setTitle(Activity activity, String title) {
        if (null != activity) {
            ActionBar actionBar = activity.getActionBar();
            if (null != actionBar) {
                actionBar.setTitle(title);
            }
        }
    }

    public void launchFragmentInActivity(Activity activity, BaseFragment fragment) {
        if (activity instanceof IFragmentLauncher) {
            ((IFragmentLauncher) activity).launchFragment(fragment);
        } else {
            throw new IllegalArgumentException("the activity can not launch the fragment");
        }
    }

    public void launchFragmentInActivity(Activity activity, int viewId, BaseFragment fragment) {
        if (activity instanceof IFragmentLauncher) {
            ((IFragmentLauncher) activity).launchFragment(viewId, fragment);
        } else {
            throw new IllegalArgumentException("the activity can not launch the fragment");
        }
    }

    public void finishCurrFragment(Activity activity) {
        if (null != activity) {
            activity.onBackPressed();
        }
    }

    public static boolean isCurrFragment(Activity activity, String tag) {
        FragmentManager fragmentManager = ((FragmentActivity)activity).getSupportFragmentManager();
        Fragment backFragment = fragmentManager.findFragmentByTag(tag);
        return backFragment.isVisible();
    }

    public void enableFragmentView(Fragment fragment, boolean enable) {
        View view = fragment.getView();
        if (null != view) {
            view.setEnabled(enable);
        }
    }

    public interface IFragmentLauncher {
        public void launchFragment(BaseFragment fragment);

        public void launchFragment(int viewId, BaseFragment fragment);
    }
}
